%% Author: Administrator
%% Created: 2009-3-21
%% Description: TODO: Add description to benchmark_app
-module(benchmark_app).

-behaviour(application).
-behaviour(supervisor).
%% --------------------------------------------------------------------
%% Include files
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% Behavioural exports
%% --------------------------------------------------------------------
%% application behaviour callback
-export([
     start/2,
     stop/1
        ]).

%% supervisor behaviour callback
-export([init/1]).

-export([start/0]).

%% --------------------------------------------------------------------
%% Internal exports
%% --------------------------------------------------------------------
-export([]).

%% --------------------------------------------------------------------
%% Macros
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% Records
%% --------------------------------------------------------------------

%% --------------------------------------------------------------------
%% API Functions
%% --------------------------------------------------------------------


%% ====================================================================!
%% External functions
%% ====================================================================!
start() ->
    application:start(benchmark).

%% --------------------------------------------------------------------
%% Func: start/2
%% Returns: {ok, Pid}        |
%%          {ok, Pid, State} |
%%          {error, Reason}
%% --------------------------------------------------------------------
start(_Type, _StartArgs) ->
    app_util:ensure_base_apps(),
%    app_util:init_logger(benchmark),
    app_util:init_default_logger("benchmark"), % benchmark should not with ring_node or other apps
 
    NodeName = list_to_atom("benchmark@" ++ app_util:get_host_addr_str()),
    net_kernel:start([NodeName, longnames]),

    RingLordNode = 
    case init:get_argument(ringlord) of
        {ok, [[RingLordStr]]} ->
            list_to_atom("ringlord@" ++ RingLordStr);
        {ok, [[]]} ->    
            'ringlord@10.0.0.253'
    end,
    true = net_kernel:connect_node(RingLordNode),
    global:sync(),
    
    case supervisor:start_link({local, benchmark_supervisor}, benchmark_app, [RingLordNode]) of
	{ok, Pid} ->
	    {ok, Pid};
	Error ->
	    Error
    end.

%% --------------------------------------------------------------------
%% Func: stop/1
%% Returns: any
%% --------------------------------------------------------------------
stop(_State) ->
    ok.

init([RingLordNode]) ->
    log4erl:debug("benchmark app start..."),
    
    Stragegy = {one_for_one, 10, 10},
    BenchServer = {
        bench_server, 
        {bench_server, start_link, []},
        transient, 1000, worker, [bench_server]
    },
    RingAccessor = {
        ring_accessor,
        {ring_accessor, start_link, [RingLordNode]},
        permanent, brutal_kill, worker, [ring_accessor]
    },

    {ok, {Stragegy, [
            BenchServer,
            RingAccessor
        ]}
    }.
    
    
%% ====================================================================
%% Internal functions
%% ====================================================================

